package com.example.permissionservice.controller;

import com.example.permissionservice.service.PermissionService;
import com.myprojects.common.pojo.Resource;
import com.myprojects.common.pojo.ResourceParent;
import com.myprojects.common.pojo.UserWithRole;
import com.example.permissionservice.service.LoginService;
import com.example.permissionservice.service.RegisterService;
import com.example.permissionservice.service.UserWithRoleService;
import com.myprojects.common.dto.Msg;
import com.myprojects.common.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Controller
public class AuthenticationController {

    @Autowired
    UserWithRoleService userWithRoleService;

    @Autowired
    LoginService loginService;

    @Autowired
    RegisterService registerService;

    /**
     * 该方法用于使用用户信息来登录
     */
//    @CrossOrigin(origins = "*", allowCredentials = "true", allowedHeaders = "*")
    @RequestMapping(value = "/login-by-info",method = RequestMethod.POST)
    @ResponseBody
    public Msg loginByUser(@Valid @RequestBody UserWithRole user, BindingResult bindingResult){
        System.out.println("loginByUser方法中user为:"+user);
        return loginService.loginByUser(user, bindingResult);
    }

    /**
     * 该方法用于使用用户信息来注册
     */
    @RequestMapping(value = "/register",method = RequestMethod.POST)
    @ResponseBody
    public Msg register(@Valid @RequestBody UserWithRole user, BindingResult bindingResult){
        return registerService.register(user, bindingResult);
    }

    /**
     * 该方法用于检查用户注册的信息
     */
    @RequestMapping(value = "/register",method = RequestMethod.PUT)
    @ResponseBody
    public Msg checkRegister(@Valid @RequestBody UserWithRole user, BindingResult bindingResult){
        Boolean bCanRegister = true;//是否允许注册的标识
        Msg result = new Msg();//尝试注册后的返回结果

        //先检查用户信息有没有错误
        Map<String,Object> errorMap = registerService.checkRegisterUser(user,bindingResult);
        //检查所有必须要的属性
        if (errorMap.get("password2") != null){
            if (!user.getPassword2().equals(user.getPassword())){
                errorMap.put("password2","二次密码不一致！");
            }
        }

        //如果有错误，则不能注册
        if (errorMap.size() > 0){
            bCanRegister = false;
            result.setCode(400);
            result.setMsg("用户信息格式错误！");
            result.addData("errorMap",errorMap);
        }

        //如果能注册，则是否允许注册的信息
        if (bCanRegister){
            result.setMsg("已通过注册验证！");
            result.setCode(200);
        }

        return result;
    }


    /**
     * 该方法用于修改密码
     */
    @RequestMapping(value = "/password",method = RequestMethod.POST)
    @ResponseBody
    public Msg updatePassword(@RequestHeader(value = "accessToken") String accessToken,
                              @Valid @RequestBody UserWithRole user,
                              BindingResult bindingResult){
        return userWithRoleService.updatePassword(accessToken,user, bindingResult);
    }

    /**
     * 该方法用于检查修改密码
     */
    @RequestMapping(value = "/password",method = RequestMethod.PUT)
    @ResponseBody
    public Msg checkUpdatePassword(@Valid @RequestBody UserWithRole user, BindingResult bindingResult){
        Msg result = new Msg();//尝试修改密码后的返回结果

        //先检查用户信息有没有错误
        Map<String,Object> errorMap = userWithRoleService.checkUpdatePassword(user,bindingResult);

        if (errorMap.size() > 0){//大于0为有问题
            result.setCode(400);
            result.setMsg("数据格式有误！");
            result.addData("errorMap",errorMap);
        }else {
            result.setCode(200);
        }

        return result;
    }

    /**
     * 该方法用于使用用户token来注销登录
     */
    @RequestMapping(value = "/logout-by-token",method = RequestMethod.POST)
    @ResponseBody
    public Msg logoutByToken(@RequestHeader("accessToken") String accessToken) {
        Msg msg = null;

        if (loginService.logoutByToken(accessToken)){
            msg = Msg.succeed();
            msg.setMsg("注销成功！");
        }else {
            msg = Msg.failed();
            msg.setMsg("该用户token不存在！");
        }
        return msg;
    }
}
